package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.TousuDto;
import com.project.webpage.dto.TousuParam;
import com.project.webpage.entity.Tousu;
import com.project.webpage.mapper.TousuMapper;
import com.project.webpage.service.TousuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TousuServiceImpl implements TousuService {

    @Resource
    private TousuMapper tousuMapper;

    @Override
    @DS("master")
    public List<TousuDto> getListForPage(TousuParam param) {
        return tousuMapper.selectListByUid(param);
    }

    @Override
    @DS("master")
    public List<Tousu> getById(Long id) {
        Example example = new Example(Tousu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id", id);
        return tousuMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(Tousu entity) {
        tousuMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void update(Tousu entity, Long idBefore) {
        Example example = new Example(Tousu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id", idBefore);
        tousuMapper.updateByExampleSelective(entity, example);
    }
}
